<template>
	<view class="addShop">
		<uni-forms :modelValue="formData" border>
			<uni-forms-item label="仓库">
				<uni-data-picker :localdata="items" popup-title="请选择仓库" @change="onchange($event)">
				</uni-data-picker>
			</uni-forms-item>
			<uni-forms-item label="仓库编码">
				<uni-easyinput type="text" v-model="formData.warehouseCode" placeholder="请输入仓库编码"
					:inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="名称">
				<uni-easyinput type="text" v-model="formData.shopName" placeholder="请输入名称" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="规格">
				<uni-easyinput type="text" v-model="formData.shopSize" placeholder="请输入规格" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="条形码">
				<uni-easyinput type="text" v-model="formData.vaseCode" placeholder="请输入盒/瓶条形码" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="外箱编码">
				<uni-easyinput type="text" v-model="formData.caseCode" placeholder="请输入外箱编码" :inputBorder="false" />
			</uni-forms-item>
			<!-- <uni-forms-item label="商品价格">
				<uni-easyinput type="text" v-model="formData.price" placeholder="请输入商品价格" :inputBorder="false" />
			</uni-forms-item> -->
			<uni-forms-item label="备注">
				<uni-easyinput type="text" v-model="formData.remarks" placeholder="请输入备注" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="数量">
				<view class="toRight">
					<uni-number-box v-model="formData.count" min="1" max="100000000"></uni-number-box>
				</view>
			</uni-forms-item>
		</uni-forms>
		<view class="buttonWrap">
			<button size="mini" type="primary" @click="handleSubmit">确认入库</button>
			<button size="mini" @click="handleCancel">取 消</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: "AddShop",
		data() {
			return {
				formData: {
					warehouseCode: "",
					shopName: "",
					shopSize: "",
					vaseCode: "",
					caseCode: "",
					price: 0,
					count: 1,
					remarks: "",
				},
				items: [],
				warehouseName: "",
				warehouseman: ""
			}
		},
		onShow() {
			this.getWarehouse()
		},
		methods: {
			// 获取仓库与仓库管理员
			async getWarehouse() {
				try {
					const {
						data
					} = await this.request("warehouseCheck", {
						orderId: ""
					})

					// 处理数据格式
					this.items = data.data.map(item => {
						item.text = item.warehouseName
						item.value = item.warehouseName
						return item
					})
				} catch (e) {
					console.log(e)
				}
			},
			onchange(e) {
				const value = e.detail.value
				this.warehouseName = value[0].text
				this.warehouseman = value[0].warehouseman
			},
			toast(title) {
				uni.showToast({
					title,
					icon: "error"
				})
			},
			// 提交验证
			submitVerify() {
				if (!this.warehouseName) {
					this.toast("请选择仓库")
					return
				}
				if (!this.formData.warehouseCode) {
					this.toast("请输入仓库编码")
					return
				}
				if (!this.formData.shopName) {
					this.toast("请输入商品名称")
					return
				}
				if (!this.formData.shopSize) {
					this.toast("请输入商品规格")
					return
				}
				if (!this.formData.vaseCode) {
					this.toast("请输入盒/瓶条形码")
					return
				}
				if (!this.formData.caseCode) {
					this.toast("请输入外箱编码")
					return
				}
				return true
			},
			async handleSubmit() {
				if (!this.submitVerify()) {
					return
				}

				await this.request("warehouseIn", {
					warehouseName: this.warehouseName,
					warehouseman: this.warehouseman,
					warehouseCode: this.formData.warehouseCode,
					shopName: this.formData.shopName,
					shopSize: this.formData.shopSize,
					vaseCode: this.formData.vaseCode,
					caseCode: this.formData.caseCode,
					count: this.formData.count,
					price: this.formData.price,
					remarks: this.formData.remarks,
				})

				uni.showToast({
					title: "操作成功",
					success() {
						uni.navigateBack()
					}
				})
			},
			handleCancel() {
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.addShop {
		padding: 20rpx;
	}
</style>
